package com.ms.p2p.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.member.Auth;
import com.ms.p2p.entity.member.Email;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping({ "/member/promotion" })
public class MemberPromotionController extends BaseController {

	@Reference
	private CertiFiCationProvider certificationProvider;
	@Reference
	private MailAuthProvider mailAuthProvider;
	@Reference
	private AdminUserProvider adminUserProvider;
	@Reference
	private MemberProvider memberProvider;
	@Reference
	private SettingProvider settingProvider;

	@Reference
	MemberAccountBankProvider memberAccountBankProvider;



	@RequestMapping("/link")
	public String link(
			HttpServletRequest request,
			HttpSession session,
			@RequestParam(value = "currentPage", required = false) Integer currentPage)
			throws Exception {
		Member loginUser = getCurrentUser();
		// 查出该用户所有的推荐用户
		PageBean page = new PageBean();
		if (currentPage != null) {
			page.setCurrentPage(currentPage);
		}
		page.setPageSize(10);
		List userList = memberProvider.findPager(null,null,null,null,null,null,null,loginUser.getId()+"",null, null,page);
		int count = memberProvider.findCount(null,null,null,null,null,null,null,loginUser.getId()+"",null, null);
		page.setTotalCount(count);
		String phone = "";
		String member_phone = loginUser.getMember_phone();
		if (member_phone != null && !member_phone.equals("")) {
			phone = getBASE64(member_phone + "");
		}
		String weburl = settingProvider.findValueByCode("weburl");
		String urlStr = "http://" + weburl + "/member/common/register?mobilePhone="
				+ phone;
		String recommend_url = "";
		recommend_url = urlStr;
		request.setAttribute("recommend_url", recommend_url);
		request.setAttribute("userList", userList);
		request.setAttribute("page", page);
		return "/customer/member/ucenter/invite/index";
	}
	/**
	 * 用BASE64加密
	 *
	 * @param str
	 * @return
	 */
	public static String getBASE64(String str) {
		byte[] b = str.getBytes();
		String s = null;
		if (b != null) {
			s = new sun.misc.BASE64Encoder().encode(b);
		}
		return s;
	}
	/**
	 * 解密BASE64字窜
	 *
	 * @param s
	 * @return
	 */
	public static String getFromBASE64(String s) {
		byte[] b = null;
		if (s != null) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				b = decoder.decodeBuffer(s);
				return new String(b);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return new String(b);
	}
}
